# __author__ = 'GuanFengyu'
import os
import subprocess
import sys
import tkinter.filedialog as fd
import tkinter.messagebox as mb
from tkinter import *
import xlwt
from myXlsStyles import styleCommon


class OrtTableToCase:
    def __init__(self):
        self.frame = Frame(width=550, height=350)
        self.text = "正交表生成测试案例"
        self.help = "Ort2xls工具使用说明"
        self.var_txt = StringVar()
        self.set_frame()

        # # test
        # self.var_txt.set('D:/WorkG/GenerateTestCase/TestData/demo.txt')
        # self.ort_table_to_case()
    
    # 选择txt文件
    def select_txt(self):
        path = fd.askopenfilename(initialdir = os.getcwd(),
                                                    filetypes = [('txt files', '*.txt')])
        self.var_txt.set(path) 

    # 正交表生成测试案例
    def ort_table_to_case(self):
        # 打开txt文件
        txtFilename = self.var_txt.get()
        if txtFilename == '':
            mb.showerror(title = "提示",message = "%s 文件路径不存在" % txtFilename)
            return 
        if not txtFilename.endswith('.txt'):
            mb.showerror(title = '提示', message = '选择文件非txt结尾')
            return
        
        # 运行PICT命令，并将结果输出到STDOUT
        cmd = "pict " + txtFilename
        # print(cmd)
        p = subprocess.Popen(args = cmd,stdout = subprocess.PIPE,shell = True)
        txtContents = p.stdout

        # 输出到Excel
        NewWb = xlwt.Workbook(encoding='utf-8')
        NewBs = NewWb.add_sheet('案例生成表',cell_overwrite_ok=True)
        NewBs.col(1).width = 256 * 80
        # 第一行标准格式
        NewBS_row0 = ['案例名称','步骤描述']
        # 输出第一行到Excel
        for i in range(0,len(NewBS_row0)):
            NewBs.write(0,i,NewBS_row0[i],styleCommon)
        
        # stdout第一行
        txtContents_line0 = txtContents.readline().decode('cp936').split()
        for i in range(len(txtContents_line0)):
            txtContents_line0[i] = '步骤' + str(i+1) + ':' + txtContents_line0[i]

        startrow = 1
        for line in txtContents:
            line_split = line.decode('cp936').split()
            line_union = list(map(lambda x :x[0]+x[1] ,zip(txtContents_line0,line_split)))
            # print(line_union)
            spe = ';\n'
            line_step = spe.join(line_union)
            NewBs.write(startrow,1,line_step,styleCommon)
            NewBs.write(startrow,0,'案例'+str(startrow))
            startrow = startrow + 1
            
        try:
            savefilename = txtFilename[:-4]+"_Case.xls"
            NewWb.save(savefilename)
            mb.showinfo(title = '提示',message = savefilename + u'文件已保存到对应目录')
        except Exception:
            mb.showerror(title = '提示', message = u'没有选择保存文件名')

    def set_frame(self):
        # 标签
        Label(self.frame,text = 'txt文件:').place(x=30, y=90)
        # 输入框entry_txt
        entry_txt = Entry(self.frame,textvariable=self.var_txt,width=43)
        entry_txt.place(x=120, y=90)
        # 按钮
        Button(self.frame,text = '浏览',command = self.select_txt,height = 1).place(x=490, y=85)
        Button(self.frame, text='生成excel案例', command=self.ort_table_to_case).place(x=230, y=200)
